﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace elr_tracking_bal.ART
{
    public class Marca : INotifyPropertyChanged
    {
        private int _ID_MARCA;
        private string _MARCA;

        public int ID_MARCA
        {
            get
            {
                return _ID_MARCA;
            }
            set
            {
                if (_ID_MARCA != value)
                {
                    _ID_MARCA = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ID_MARCA"));
                    }
                }

            }
        }

        public string MARCA
        {
            get
            {
                return _MARCA;
            }
            set
            {
                if (_MARCA != value)
                {
                    _MARCA = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("MARCA"));
                    }
                }

            }
        }

        /// <summary>
        /// Método que ejecuta el store para recuperar las Marcas
        /// </summary>
        /// <returns></returns>
        public static DataTable GetData()
        {
            DataTable dt;
            dt = elr_tracking_dal.SrvDB.ExecuteQuery("SP_GETLIST_ART_MARCA");
            return dt;
        }
        /// <summary>
        /// Método para recuperar la lista de Marcas
        /// </summary>
        /// <returns></returns>
        public static List<Marca> GetDataMarca()
        {
            try
            {
                DataTable dt;
                dt = Marca.GetData();
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    List<Marca> list = dt.AsEnumerable().Select(
                        row => new  Marca
                        {
                            ID_MARCA = row.Field<int?>("ID_MARCA").GetValueOrDefault(),
                            MARCA = String.IsNullOrEmpty(row.Field<string>("MARCA")) ? "" : row.Field<string>("MARCA"),
                        }
                        ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Método que inserta una marca
        /// </summary>
        /// <param name="art"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg InsertMarca(Marca art)
        {
            Marca inser;
            UpsertStateMsg inserart;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_INSERT_ART_MARCA '" + art.MARCA+ "'");

                inser = new Marca(art.MARCA);
                inserart = new UpsertStateMsg("Registros guardados " + dt.ToString(), true);
                return inserart;
            }
            catch (Exception ex)
            {
                inserart= new UpsertStateMsg("Error al guardar " + ex, false);
                return inserart;
            }
        }
        /// <summary>
        /// Método que borra varias Marcas
        /// </summary>
        /// <param name="ID_MARCA"></param>
        /// <returns></returns>
        public static elr_tracking_bal.DeleteStateMsg DeleteMarca(string[] ID_MARCA)
        {
            elr_tracking_bal.DeleteStateMsg delete;
            string join;
            if (ID_MARCA== null)
            {
                join = "";
            }
            else
            {
                join = String.Join(",", ID_MARCA);
            }
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_DELETE_ART_MARCA '" + join + "'");
                delete = new elr_tracking_bal.DeleteStateMsg("Registro eliminado" + dt.ToString(), true);
                return delete;
            }
            catch (Exception ex)
            {
                delete = new elr_tracking_bal.DeleteStateMsg("Error al eliminar" + ex, false);
                return delete;
            }
        }
        /// <summary>
        /// Método que modifica una marca
        /// </summary>
        /// <param name="art"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg UpdateMarca(Marca art)
        {
            Marca update;
            UpsertStateMsg updateCat;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_UPDATE_ART_MARCA " +art.ID_MARCA + ",'" + art.MARCA + "'");

                update = new Marca(art.ID_MARCA, art.MARCA);
                updateCat = new UpsertStateMsg("Registros actualizados " + dt.ToString(), true);
                return updateCat;
            }
            catch (Exception ex)
            {
                updateCat = new UpsertStateMsg("Error al actualizar " + ex, false);
                return updateCat;
            }
        }
        /// <summary>
        /// Método que obtiene una marca
        /// </summary>
        /// <param name="ID_MARCA"></param>
        /// <returns></returns>
        public static Marca GetOneMarca(int ID_MARCA)
        {
            try
            {
                Marca art;
                DataTable dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_GETONE_ART_MARCA " + ID_MARCA + "");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (ID_MARCA == 0)
                {
                    return null;
                }
                else
                {
                    art = new Marca(Convert.ToInt32(dt.Rows[0]["ID_MARCA"]), Convert.ToString(dt.Rows[0]["MARCA"]));
                }
                return art;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Constructor sin parámetros
        /// </summary>
        public Marca() { }

        public Marca(string MARCA) {
            this._MARCA = MARCA;
        }
        /// <summary>
        /// Constructor con parámetros
        /// </summary>
        /// <param name="ID_MARCA"></param>
        /// <param name="MARCA"></param>
        public Marca(int ID_MARCA, string MARCA)
        {
            this._ID_MARCA = ID_MARCA;
            this._MARCA = MARCA;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

